import os, random
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLineEdit, QPushButton, QFileDialog, QMessageBox
from widgets.avatar_label import AvatarLabel
from widgets.countdown_button import CountdownButton
from db_utils import add_user

class RegisterDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('注册新账号')
        self.resize(420, 320)
        layout = QVBoxLayout(self)

        av_layout = QHBoxLayout()
        self.avatar = AvatarLabel(80)
        self.btn_upload_avatar = QPushButton('上传头像')
        self.btn_upload_avatar.clicked.connect(self._on_upload_avatar)
        av_layout.addWidget(self.avatar)
        av_layout.addWidget(self.btn_upload_avatar)
        layout.addLayout(av_layout)

        self.le_username = QLineEdit(); self.le_username.setPlaceholderText('用户名')
        self.le_phone = QLineEdit(); self.le_phone.setPlaceholderText('手机号')
        self.le_code = QLineEdit(); self.le_code.setPlaceholderText('验证码')
        self.btn_get_code = CountdownButton('获取验证码')
        self.btn_get_code.clicked.connect(self._on_get_code)
        h_code = QHBoxLayout(); h_code.addWidget(self.le_code); h_code.addWidget(self.btn_get_code)

        self.le_new_pwd = QLineEdit(); self.le_new_pwd.setPlaceholderText('新密码'); self.le_new_pwd.setEchoMode(QLineEdit.Password)
        self.le_new_pwd2 = QLineEdit(); self.le_new_pwd2.setPlaceholderText('重复新密码'); self.le_new_pwd2.setEchoMode(QLineEdit.Password)

        layout.addWidget(self.le_username)
        layout.addWidget(self.le_phone)
        layout.addLayout(h_code)
        layout.addWidget(self.le_new_pwd)
        layout.addWidget(self.le_new_pwd2)

        self.btn_submit = QPushButton('提交')
        self.btn_submit.clicked.connect(self._on_submit)
        layout.addWidget(self.btn_submit)

        self._sent_code = None

    def _on_upload_avatar(self):
        path, _ = QFileDialog.getOpenFileName(self, '选择头像', os.getcwd(), 'Images (*.png *.jpg *.jpeg)')
        if path:
            self.avatar.set_avatar(path)

    def _on_get_code(self):
        phone = self.le_phone.text().strip()
        if not phone:
            QMessageBox.warning(self, '错误', '请输入手机号')
            return
        self._sent_code = f'{random.randint(100000,999999)}'
        QMessageBox.information(self, '验证码', f'验证码为: {self._sent_code}（演示）')
        self.btn_get_code.start_countdown(60)

    def _on_submit(self):
        username = self.le_username.text().strip()
        phone = self.le_phone.text().strip()
        code = self.le_code.text().strip()
        pwd = self.le_new_pwd.text()
        pwd2 = self.le_new_pwd2.text()
        avatar = self.avatar.avatar_path()
        if not all([username, phone, code, pwd, pwd2]):
            QMessageBox.warning(self, '错误', '请完整填写信息')
            return
        if code != self._sent_code:
            QMessageBox.warning(self, '错误', '验证码错误')
            return
        if pwd != pwd2:
            QMessageBox.warning(self, '错误', '两次密码不一致')
            return
        ok, msg = add_user(username=username, phone=phone, account=phone, password=pwd, avatar_path=avatar)
        if not ok:
            QMessageBox.warning(self, '注册失败', msg)
            return
        QMessageBox.information(self, '成功', '注册成功')
        self.accept()
